package sgi.client;

import java.io.IOException;
import java.sql.SQLException;

import sgi.message.MessageCreateReport;
import sgi.message.MessageRetrieveReport;
import sgi.misc.*;

import java.sql.ResultSet;
import java.util.Calendar;
import java.util.Date;
import java.sql.SQLException;

import javax.sql.rowset.*;

import com.sun.rowset.CachedRowSetImpl;

public class SGICheckReportsController extends SGIController {
	
	public static SGICheckReportsController checkReports;
	public static SGICheckReportsUI checkReportsUI;
	
	public static SGICheckReportsController create() {
		if( SGICreateReportsController.createReports == null )
		{
			SGICheckReportsController.checkReports = new SGICheckReportsController();
			checkReportsUI.setController(checkReports);
		}
		
		SGICheckReportsController.checkReportsUI.setVisible(true);
		return SGICheckReportsController.checkReports;
	}

	public SGICheckReportsController() {
		checkReportsUI = new SGICheckReportsUI();
	}
	
	
	@SuppressWarnings("deprecation")
	public void report(Date date, int reportType) throws SQLException {
		String query = null;
		
		switch (reportType) {
		case 0://daily report
			query = "select * from daily_statistics where date = '"+(((int)date.getYear()+1900))+"-"+((int)date.getMonth()+1)+"-"+((int)date.getDate())+"'";
			break;
		case 1://monthly complaints report
			query = "select * from monthly_complaint_status where MONTH(date) = "+(int)date.getMonth()+"+1 and YEAR(DATE) = "+(int)date.getYear()+"+1900";
			break;
		case 2://monthly database status report
			query = "select * from database_reports where MONTH(date) = "+(int)date.getMonth()+"+1 and YEAR(DATE) = "+(int)date.getYear()+"+1900";
			break;
		case 3://monthly package info report
			query = "select * from imagepack_sub_reports where MONTH(date) = "+(int)date.getMonth()+"+1 and YEAR(DATE) = "+(int)date.getYear()+"+1900";
			break;
		case 4://weekly statistical info
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
			query = "select * from daily_statistics where YEAR(date) = "+(int)date.getYear()+"+1900 and WEEK(date) = "+weekOfYear+"-1";
			break;
		case 5://yearly statistical info
			query = "select * from imagepack_sub_reports where YEAR(date) = "+(int)date.getYear()+"+1900";
			break;
		default:
			break;
		}
		MessageRetrieveReport msg = new MessageRetrieveReport(date, query, reportType);
		retrieveReport(msg);
	}
	
	private void retrieveReport(MessageRetrieveReport msg) {
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	

	@SuppressWarnings("unused")
	@Override
	void handleMessage(Object msg) {
		try {
		CachedRowSetImpl info;
			info = new CachedRowSetImpl();
			info.populate(((MessageRetrieveReport)msg).getRS());
			if (info==null)
				return;
			switch (((MessageRetrieveReport)msg).getReportType())
			{
			case 0:
				DailyReportPanel dailyReport = new DailyReportPanel(info);
				dailyReport.setVisible(true);
				break;
			case 1:
				MonthlyComplaintsReportPanel complaintsReport = new MonthlyComplaintsReportPanel(info);
				complaintsReport.setVisible(true);
				break;
			case 2:
				MonthlyDatabaseReportPanel databaseReport = new MonthlyDatabaseReportPanel(info);
				databaseReport.setVisible(true);
				break;
			case 3:
				MonthlyImagepackSubReportPanel imagepackReport = new MonthlyImagepackSubReportPanel(info);
				imagepackReport.setVisible(true);
				break;
			case 4:
				WeeklyReportPanel weeklyReport = new WeeklyReportPanel(info);
				weeklyReport.setVisible(true);
				break;
			case 5:
				//DailyReportPanel dailyReport = new DailyReportPanel(info);
				break;
			default:
				
				break;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public static void terminate()
	{
		SGICheckReportsController.checkReports = null;
		SGICheckReportsController.checkReportsUI.setController(null);
		SGICheckReportsController.checkReportsUI.setVisible(false);
		SGICheckReportsController.checkReportsUI = null;
		SGIUserAccountMenuController.create(null);
	}
	
}
